import 'package:flutter/cupertino.dart';
import 'package:flutter_09_game/base/controller/base_list_more_controller.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/http/http_export.dart';
import 'package:flutter_09_game/pages/deposit/utils_deposit.dart';
import 'package:flutter_09_game/pages/profile/profile_bet_record/models/model_game_round_join.dart';
import 'package:flutter_09_game/pages/profile/profile_bet_record/models/model_third_game_list.dart';
import 'package:flutter_09_game/pages/profile/profile_bet_record/utils_bet_record.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart';
import 'package:get/get.dart';

class ProfileBetRecordGameLogic extends BaseListMoreController {
  List<String> gameRows = [LocaleKeys.allGames.tr];

  RxString timeRx = UtilsBetRecord.timeRows.first.obs;

  RxString statusRx = UtilsBetRecord.statusRowsNoing.first.obs;
  late RxString gameRx = gameRows.first.obs;
  RxString selectorTagRx = UtilsBetRecord.options.first.obs;

  TextEditingController userIdController = TextEditingController();
  TextEditingController codeController = TextEditingController();

  // 日期范围相关属性
  Rx<DatePeriod?> selectedDateRange = Rx<DatePeriod?>(null);
  List<ModelThirdGameList> games = [];
  ModelGameRoundJoin roundJoin = ModelGameRoundJoin();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    everAll([gameRx, statusRx, selectorTagRx], (val) {
      refreshController.requestRefresh();
    });
    getThirdGameList();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    userIdController.dispose();
    codeController.dispose();
  }

  @override
  Future<List> loadData() async {
    // TODO: implement loadData
    final dateRange = UtilsDeposit.calculateDateRange(timeRx.value,
        selectedDateRange: selectedDateRange.value);
    HttpResponse response = await HttpManager().get(API.getThirdGameLog,
        params: {
          'game_code': games
                  .firstWhereOrNull((item) => item.gameName == gameRx.value)
                  ?.gameCode ??
              '',
          'status': UtilsBetRecord.statusRowsNoing.indexOf(statusRx.value),
          'user_identity': UtilsBetRecord.options.indexOf(selectorTagRx.value) == 0
              ? null
              : userIdController.text,
          'transaction_code': codeController.text,
          'start': dateRange['start'],
          'end': dateRange['end'],
          'page': pageNum,
          'size': pageSize,
          'is_team': UtilsBetRecord.options.first == selectorTagRx.value
              ? false
              : true,
        },
        fromJson: ModelGameRoundJoin.fromJson);
    if (response.isSuccess) {
      roundJoin = response.data;
      (roundJoin.stats ?? []).forEach((item) {
        item.symbol = 'TRX';
      });
    }
    return roundJoin.gameRoundJoin ?? [];
    // return [1];
    // return super.loadData();
  }

  getThirdGameList() async {

    HttpResponse response = await HttpManager()
        .get(API.getThirdGameList, fromJson: ModelThirdGameList.fromJson);
    if (response.isSuccess) {
      games = response.data;
      for (var item in games) {
        gameRows.add((item.gameName ?? '').isNotEmpty
            ? (item.gameName ?? '')
            : LocaleKeys.unnamedGame.tr);
      }
      update();
    }
  }

  /// 更新时间选择
  void updateTimeSelection(String timeType) {
    if (timeType == LocaleKeys.custom.tr) {
      UtilsDeposit.showDateRangePicker(selectedDateRange.value).then((value) {
        if (value is DatePeriod) {
          selectedDateRange.value = value;
          refreshController.requestRefresh();
          timeRx.value = timeType;
        }
      });
    } else {
      timeRx.value = timeType;
      // 清除自定义日期范围
      selectedDateRange.value = null;
      refreshController.requestRefresh();
    }
  }
}
